﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CofeCore = Cofe.Core;

namespace Cofe.Core.Property
{
    public class PropertyProvider : PropertyProviderBase
    {
        #region Constructor

        private PropertyProvider(List<PropertyPair> propertyPairs)
        {
            foreach (var pp in propertyPairs)
                registerProperties(pp.Property, () => pp.Value);
        }

        public static IPropertyProvider FromPropertyPairs(List<PropertyPair> propertyPairs)
        {
            return new PropertyProvider(propertyPairs);
        }

        public static IPropertyProvider ForUserRootDirectory()
        {
            return new UserRootPropertyProvider();
        }

        #endregion

    }

    public class UserRootPropertyProvider : PropertyProviderBase
    {
        #region Constructor

        public UserRootPropertyProvider()
            : base(CofeCore.PropertyDefinitions.CofeDefinitions, 
                CofeCore.PropertyDefinitions.CacheDefinitions,
                CofeCore.PropertyDefinitions.CofeDirDefinitions)
        {
            registerProperties(CofeProperties.FullParseName, () => "");
            registerProperties(CofeProperties.Parent, () => null);
            registerProperties(CofeProperties.Label, () => "");
            registerProperties(CofeProperties.Type, () => EntryTypes.CofeDirectoryInfo);
            registerProperties(CofeProperties.DefaultInterfaceType, () => typeof(ICofeDirectoryInfo));
            registerProperties(CacheProperties.Id, () => CofeConstants.RootGuid);
            Func<ParameterDic, IEnumerable<ICofeSystemInfo>> syncList = (pmDic) => { pmDic.IsHandled = true; return list(); };
            registerAction(CofeDirectoryProperties.List, syncList);
        }
        
        #endregion

        #region Methods

        private IEnumerable<ICofeSystemInfo> list()
        {
            foreach (var rph in CofeServices.VolumeRegistrar.Behaviors.GetRootPropertyHosts(EntryConstructionFlags.Default))
            {
                if (CofeServices.SecurityManager.IsPermissionGranted(rph, Security.PermissionType.Read))
                {                    
                    yield return CofeServices.EntryConstructor.ConstructEntry(rph);
                }
            }            
        }

        #endregion
    }
}
